package demo1;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/10/27
 * Time: 9:11
 * Description:
 */

class Animal {
    protected String name;
    private int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
        //eat();坑
    }

    public void eat() {
        System.out.println(name+" eat()");
    }
}

class Dog extends Animal{

    /*public Dog (){
        super();
    }*/

    public Dog(String name,int age) {
        super(name,age);
    }

   /* public String name="fasasafa";//优先调用自己的

    public void test() {
        //System.out.println(super.name);
        //super.eat();
    }*/
   public void eat() {
        System.out.println("Dog::eat()");
   }
}

class Bird extends Animal{

    public String selfFiled;

    public Bird(String name, int age, String selfFiled) {
        super(name, age);
        this.selfFiled = selfFiled;
    }

    public void eat() {
        System.out.println("Bird::eat()");
    }

    public void fly() {
        System.out.println(name+"fly()");
    }

}

public class TestDemo {

    /**
     * 构造方法  调用重写
     * @param args
     */
    public static void main(String[] args) {
        Dog dog = new Dog("xiaogou",14);

    }

    public static void func(Animal animal) {
        animal.eat();
    }

    public static Animal func2() {
        Dog dog = new Dog("xiaogou",14);
        return dog;
    }

    public static void main7(String[] args) {
        Dog dog = new Dog("xiaogou",14);
        func(dog);

        Bird bird = new Bird("小鸟",18,"self");
        func(bird);
    }



    public static void main6(String[] args) {
        //向上转型：父类引用   引用 子类对象
        Animal animal = new Dog("xiaogou",14);
        animal.eat();

        /*Animal animal2 = new Bird("xiaogou",14,"fsafa");
        animal2.eat();*/
    }


    /**
     * 向下转型
     * @param args
     */
    public static void main5(String[] args) {
        Animal animal2 = new Bird("小鸟",18,"self");
        animal2.eat();
        //向下转型
        Bird bird = (Bird)animal2;
        bird.fly();

        System.out.println("=========向下转型不安全========");
        Animal animal = new Dog("xiaogou",14);
        //向下转型 但是不安全的
        if(animal instanceof Bird) {
            Bird bird2 = (Bird)animal;
            bird2.fly();
        }
    }


    public static void main4(String[] args) {
        /*Dog dog = new Dog("xiaogou",14);
        Animal animal = dog;*/

        //向上转型：父类引用   引用 子类对象
        Animal animal = new Dog("xiaogou",14);
        animal.eat();
        System.out.println("==================");
        Animal animal2 = new Bird("小鸟",18,"self");
        animal2.eat();
        animal2.name = "fafafsfa";
        /*animal2.fly();
        animal2.selfFiled="fsfa";*/
    }


    public static void main3(String[] args) {
        Dog dog = new Dog("xiaogou",14);
        dog.eat();
    }


   /* public static void main2(String[] args) {
        Bird bird = new Bird();
        bird.name = "小鸟";
        bird.eat();
    }
    public static void main1(String[] args) {
        Dog dog = new Dog();
        dog.name = "长江7号";
        dog.eat();
    }*/
}
